import javapns.devices.Device;
import javapns.devices.implementations.basic.BasicDevice;
import javapns.notification.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: IosPush
 * @Author: 王延
 * @Date: 2019/7/31 17:09
 * @Description :
 */
public class IosPush {
    public static void main(String[] args) throws Exception {
        String msgCertificatePassword = "dipingjieTP";//导出证书时设置的密码
        String deviceToken = "3b52db54 595431b5 f452f214 7860d8ce 7a6db4ff f4e179d1 d05a75c7 42f3ed0c"; //手机设备token号
        String message = "fuck it";

        /**
         * 取path
         */
        //Thread.currentThread().getContextClassLoader().getResources("config.json")
        //String path = this.getClass().getClassLoader().getResource("/").getPath();
        //String path = IosPush.class.getClassLoader().getResource("/").getPath();
//        String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
//         String filePath = java.net.URLDecoder.decode(path,"utf-8");
//        String certificatePath =  filePath+"/dipingjie.p12";
        String certificatePath = "G:\\myEmpty\\demo\\src\\main\\resources\\dipingjie.p12";
        //推送体
        PushNotificationPayload payload = new PushNotificationPayload();
        payload.addAlert(message); // 消息内容
        payload.addBadge(1);
        payload.addCustomDictionary("uid", "haahi");
        payload.addCustomDictionary("type", 12);
        payload.addCustomDictionary("title", "haahi");
        payload.addSound("default");// 铃音

        //推送管理器
        PushNotificationManager pushManager = new PushNotificationManager();
        //初始化推送管理器 true表示测试,false表示生产
        pushManager.initializeConnection(new AppleNotificationServerBasicImpl(
                certificatePath, msgCertificatePassword, true));

        List<PushedNotification> notifications = new ArrayList<PushedNotification>();
        // 开始推送消息
        List<String> tokens = new ArrayList<String>();
        tokens.add(deviceToken);
        //推送单条
        boolean sendCount = true;
        if (true) { //推单条
            Device device = new BasicDevice();
            device.setToken(deviceToken);
            PushedNotification notification = pushManager.sendNotification(
                    device, payload, false);
            notifications.add(notification);
        } else {
            //多条推送
            List<Device> devices = new ArrayList<Device>();
            for (String token : tokens) {
                devices.add(new BasicDevice(token));
            }
            notifications = pushManager.sendNotifications(payload, devices);
        }
        //失败条目
        List<PushedNotification> failedNotification = PushedNotification
                .findFailedNotifications(notifications);
        //成功条目
        List<PushedNotification> successfulNotification = PushedNotification
                .findSuccessfulNotifications(notifications);
        int failed = failedNotification.size();
        int successful = successfulNotification.size();
        System.out.println("zsl==========成功数：" + successful);
        System.out.println("zsl==========失败数：" + failed);
        //关闭推送连接
        pushManager.stopConnection();
        System.out.println("zsl==========消息推送完毕");
    }
}
